package com.zwy.ubts.utils;


import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class AES {
    private static final String ENCRYPTION = PropertiesFile.getKey("mail.likename");

    /**
     * 获得一个 密钥长度为 256 位的 AES 密钥，
     * @return 返回经 BASE64 处理之后的密钥字符串
     */
    private static String getStrKeyAES(String code) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = new SecureRandom(code.getBytes("utf-8"));
        keyGen.init(128, secureRandom);   // 这里可以是 128、192、256、越大越安全
        SecretKey secretKey = keyGen.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    /**
     *  将使用 Base64 加密后的字符串类型的 secretKey 转为 SecretKey
     * @param strKey
     * @return SecretKey
     */
    private static SecretKey strKey2SecretKey(String strKey){
        byte[] bytes = Base64.getDecoder().decode(strKey);
        SecretKeySpec secretKey = new SecretKeySpec(bytes, "AES");
        return secretKey;
    }

    /**
     * 加密
     * @param content 待加密内容
     * @param secretKey 加密使用的 AES 密钥
     * @return 加密后的密文 byte[]
     */
    private static byte[] encryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(content);
    }

    /**
     * 解密
     * @param content 待解密内容
     * @param secretKey 解密使用的 AES 密钥
     * @return 解密后的明文 byte[]
     */
    private static byte[] decryptAES(byte[] content, SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(content);
    }


    public static String encryption(String text){
        try {
            String strKeyAES = AES.getStrKeyAES(ENCRYPTION);
            SecretKey secretKey = AES.strKey2SecretKey(strKeyAES);
            byte[] encryptAESbytes = AES.encryptAES(text.getBytes("utf-8"), secretKey);
            return Base64.getEncoder().encodeToString(encryptAESbytes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "err1";
    }

    public static String decrypt(String text){
        try {
            String strKeyAES = AES.getStrKeyAES(ENCRYPTION);
            SecretKey secretKey = AES.strKey2SecretKey(strKeyAES);
            byte[] encryptAESbytes = Base64.getDecoder().decode(text);
            String decryptAESStr = new String(AES.decryptAES(encryptAESbytes, secretKey), "utf-8");
            return decryptAESStr;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "err2";
    }


//    public static void main(String[] args){
////        String content = "abcdefg789+-*+="; // 待加密的字符串
////        System.out.println("明文数据为：" + content);
////        try {
////            // 获得经 BASE64 处理之后的 AES 密钥
////            String strKeyAES = AES.getStrKeyAES("加密");
////            System.out.println("经BASE64处理之后的密钥：" + strKeyAES);
////
////            // 将 BASE64 处理之后的 AES 密钥转为 SecretKey
////            SecretKey secretKey = AES.strKey2SecretKey(strKeyAES);
////            System.out.println("secretKey:"+secretKey);
////
////            // 加密数据
////            byte[] encryptAESbytes = AES.encryptAES(content.getBytes("utf-8"), secretKey);
////            System.out.println("加密后的数据经 BASE64 处理之后为：" + Base64.getEncoder().encodeToString(encryptAESbytes));
////            // 解密数据
////            String decryptAESStr = new String(AES.decryptAES(encryptAESbytes, secretKey), "utf-8");
////            System.out.println("解密后的数据为：" + decryptAESStr);
////
////            if (content.equals(decryptAESStr)){
////                System.out.println("测试通过！");
////            }else {
////                System.out.println("测试未通过！");
////            }
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//    }



}
